/**
 * 建立一个工程实现多个页面的跳转,传值,回传值
 */
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:flutter/src/material/raised_button.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(title: "flutter", home: new RandomWords());
  }
}

class RandomWords extends StatefulWidget {
  @override
  createState() => new RandomWordsState();
}

class RandomWordsState extends State<RandomWords> {
  @override
  String backValue;
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('home'),
        actions: <Widget>[
          new IconButton(
            icon: new Icon(Icons.list),
            // onPressed: _pushSaved, // 方法一
            // 方法二
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return Page2(
                  text: "我是home传过来的参数",
                );
              })).then((back) {
                setState(() {
                  backValue = back;
                });
              });
            },
          )
        ],
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Text('$backValue'),
            RaisedButton(
                onPressed: () => Navigator.push(context,
                        MaterialPageRoute(builder: (context) {
                      return Page2(
                        text: "我是home传过来的参数",
                      );
                    })).then((back) {
                      setState(() {
                        backValue = back;
                      });
                    }),
                child: Text('页面跳转传值'))
          ],
        ),
      ),
    );
  }

  void _pushSaved() {
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text('page two'),
          actions: <Widget>[
            new IconButton(
              icon: new Icon(Icons.list),
              onPressed: null,
            )
          ],
        ),
        body: Center(
          child: Text("This is page two"),
        ),
      );
    }));
  }
}

class Page2 extends StatelessWidget {
  Page2({
    Key key,
    @required this.text, // 接收一个text参数
  }) : super(key: key);
  final String text;
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('page two'),
          actions: <Widget>[
            new IconButton(
              icon: new Icon(Icons.list),
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return Page3();
                }));
              },
            )
          ],
        ),
        body: Padding(
            padding: EdgeInsets.all(18),
            child: Center(
              child: Column(
                children: <Widget>[
                  Text(text),
                  RaisedButton(
                      onPressed: () => Navigator.pop(context, '我是子页面传过来的数据'),
                      child: Text('返回传值'))
                ],
              ),
            )));
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('page three'),
      ),
    );
  }
}
